<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <!-- 图标 -->
      <el-col :span="3" class="left">
        <img src="//s0.meituan.net/bs/fe-web-meituan/fa5f0f0/img/logo.png" alt="美团">
      </el-col>
      <!-- 搜索框及导航栏 -->
      <el-col :span="15" class="center">
        <!-- 搜索框 -->
        <div class="wrapper">
          <el-input
            v-model="search"
            placeholder="搜索商家或地点"
            @focus="focus"
            @blur="blur"
            @input="input"
          />
          <button class="el-button el-button--primary">
            <i class="el-icon-search" />
          </button>
          <!-- 搜索历史 -->
          <dl v-if="isSearchHistory" class="searchHistory">
            <h6>最近搜索</h6>
            <span>删除搜索历史</span>
            <dd>
              <a
                  v-for="(item,idx) in $store.state.home.hotPlace.slice(0,5)"
                  :key="idx"
                  :href="'/products?keyword='+encodeURIComponent(item.name)">{{ item.name }}</a>
            </dd>
          </dl>
          <!-- 搜索结果 -->
          <dl v-if="isSearchList" class="searchList">
            <dd v-for="(item, index) in searchList" :key="index">
              <a :href="'/products?keyword='+encodeURIComponent(item.name)">{{ item.name }}</a>
            </dd>
          </dl>
        </div>
        <!-- 搜索框底部导航栏 -->
        <ul class="nav">
          <li>
            <nuxt-link to="/" class="takeout">
              美团外卖
            </nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="movie">
              猫眼电影
            </nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="hotel">
              美团酒店
            </nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="apartment">
              民宿/公寓
            </nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="business">
              商家入驻
            </nuxt-link>
          </li>
          <li>
            <nuxt-link to="/" class="welfare">
              美团公益
            </nuxt-link>
          </li>
        </ul>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import _ from 'lodash'
export default {
  data () {
    return {
      search: '',
      isFocus: false,
      searchList: [],
      searchHistory: []
    }
  },
  computed: {
    isSearchHistory () {
      return this.isFocus && !this.search
    },
    isSearchList () {
      return this.isFocus && this.search
    }
  },
  methods: {
    focus () {
      this.isFocus = true
    },
    blur () {
      const self = this
      setTimeout(function () {
        self.isFocus = false
      },200)
    },
    input: _.debounce(async function() {
      this.searchList = []
      let {top} = await this.$axios.get('/search/top', {params: {input: this.search}})
      this.searchList = top.slice(0,10)
    },200)
  }
}
</script>

<style>

</style>
